package com.jxdinfo.hussar.example.sbjx.equiprepair.service.impl;

import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.example.sbjx.equiprepair.model.EquipRepair;
import com.jxdinfo.hussar.example.sbjx.equiprepair.service.EquipRepairService;
import com.jxdinfo.hussar.example.sbjx.equiprepair.dao.EquipRepairMapper;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import com.jxdinfo.hussar.example.sbjx.equiprepair.vo.EquipRepairPageVO;
import com.jxdinfo.hussar.example.sbjx.equiprepair.dto.EquipRepairSelectCondition;
import com.jxdinfo.hussar.common.tfmodel.TfModelEnum;
import com.jxdinfo.hussar.support.transaction.core.annotation.HussarTransactional;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import com.jxdinfo.hussar.support.mp.base.query.generator.superquery.SuperQueryGenerator;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import java.util.ArrayList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.jxdinfo.hussar.support.mp.base.query.dto.QueryConditionDto;
import com.jxdinfo.hussar.support.mp.base.query.dto.SuperQueryConditionDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxdinfo.hussar.example.sbjx.equiprepair.dto.EquipRepairEquiprepairdataset1;

/**
 * 设备检修表服务实现类
 */
@Service("sbjx.equiprepair.EquipRepairServiceImpl")
@HussarDs("master")
public class EquipRepairServiceImpl extends HussarServiceImpl<EquipRepairMapper, EquipRepair> implements EquipRepairService {

    private static final Logger logger = LoggerFactory.getLogger(EquipRepairServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private EquipRepairMapper equipRepairMapper;

    @Override
    @HussarTransactional
    public ApiResponse<String> insertOrUpdate(EquipRepair equipRepair) {
        try {
            this.saveOrUpdate(equipRepair);
            return ApiResponse.success(String.valueOf(equipRepair.getId()), "");
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_ADD_OR_MODIFY.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepair> formQuery(String id) {
        try {
            return ApiResponse.success(this.getById(id));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> hussarQueryPage(Page<EquipRepair> page) {
        try {
            Page<EquipRepair> pager = new Page<>(page.getCurrent(), page.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (page.orders() != null) {
                for (OrderItem orderItem : page.orders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            List<EquipRepair> list = this.page(pager, queryWrapper).getRecords();
            equipRepairPageVO.setData(list);
            equipRepairPageVO.setCount(pager.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> hussarQueryPage_order_custom(Page<EquipRepair> page) {
        try {
            Page<EquipRepair> pager = new Page<>(page.getCurrent(), page.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (page.orders() != null) {
                for (OrderItem orderItem : page.orders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            List<EquipRepair> list = this.page(pager, queryWrapper).getRecords();
            equipRepairPageVO.setData(list);
            equipRepairPageVO.setCount(pager.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<Boolean> del(List<String> ids) {
        try {
            return ApiResponse.success(this.removeByIds(ids));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_DELETION_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> hussarQueryequipRepairCondition_1Page(EquipRepairEquiprepairdataset1 equiprepairdataset1) {
        try {
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            Page<EquipRepair> pager = new Page<>(equiprepairdataset1.getCurrent(), equiprepairdataset1.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (equiprepairdataset1.getOrders() != null) {
                for (OrderItem orderItem : equiprepairdataset1.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            equiprepairdataset1.setEquipRepairequipNameFullLike(equiprepairdataset1.getEquipRepairequipNameFullLike().replaceAll("/", "//").replaceAll("%", "/%").replaceAll("_", "/_").replaceAll("\\[", "/[").replaceAll("]", "/]"));
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            List<EquipRepair> list = equipRepairMapper.hussarQueryequipRepairCondition_1Page(pager, equiprepairdataset1, queryWrapper);
            equipRepairPageVO.setData(list);
            equipRepairPageVO.setCount(pager.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> hussarQueryequipRepairCondition_1Page_order_custom(EquipRepairEquiprepairdataset1 equiprepairdataset1) {
        try {
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            Page<EquipRepair> pager = new Page<>(equiprepairdataset1.getCurrent(), equiprepairdataset1.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (equiprepairdataset1.getOrders() != null) {
                for (OrderItem orderItem : equiprepairdataset1.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            equiprepairdataset1.setEquipRepairequipNameFullLike(equiprepairdataset1.getEquipRepairequipNameFullLike().replaceAll("/", "//").replaceAll("%", "/%").replaceAll("_", "/_").replaceAll("\\[", "/[").replaceAll("]", "/]"));
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            List<EquipRepair> list = equipRepairMapper.hussarQueryequipRepairCondition_1Page_order_custom(pager, equiprepairdataset1, queryWrapper);
            equipRepairPageVO.setData(list);
            equipRepairPageVO.setCount(pager.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> ConditionFilterPage(EquipRepairSelectCondition equipRepairSelectCondition) {
        try {
            Page<EquipRepair> pager = new Page<>(equipRepairSelectCondition.getCurrent(), equipRepairSelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (equipRepairSelectCondition.getOrders() != null) {
                for (OrderItem orderItem : equipRepairSelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            if (HussarUtils.isNotEmpty(equipRepairSelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = equipRepairSelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<EquipRepair> superQueryGenerator = new SuperQueryGenerator(EquipRepair.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<EquipRepair> list = this.page(pager, queryWrapper);
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            equipRepairPageVO.setData(list.getRecords());
            equipRepairPageVO.setCount(list.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> ConditionFilterPage_order_custom(EquipRepairSelectCondition equipRepairSelectCondition) {
        try {
            Page<EquipRepair> pager = new Page<>(equipRepairSelectCondition.getCurrent(), equipRepairSelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (equipRepairSelectCondition.getOrders() != null) {
                for (OrderItem orderItem : equipRepairSelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            if (HussarUtils.isNotEmpty(equipRepairSelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = equipRepairSelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<EquipRepair> superQueryGenerator = new SuperQueryGenerator(EquipRepair.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<EquipRepair> list = this.page(pager, queryWrapper);
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            equipRepairPageVO.setData(list.getRecords());
            equipRepairPageVO.setCount(list.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> ConditionFilterequipRepairCondition_2Page(EquipRepairSelectCondition equipRepairSelectCondition) {
        try {
            Page<EquipRepair> pager = new Page<>(equipRepairSelectCondition.getCurrent(), equipRepairSelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (equipRepairSelectCondition.getOrders() != null) {
                for (OrderItem orderItem : equipRepairSelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            if (HussarUtils.isNotEmpty(equipRepairSelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = equipRepairSelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<EquipRepair> superQueryGenerator = new SuperQueryGenerator(EquipRepair.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<EquipRepair> list = this.page(pager, queryWrapper);
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            equipRepairPageVO.setData(list.getRecords());
            equipRepairPageVO.setCount(list.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> ConditionFilterequipRepairCondition_2Page_order_custom(EquipRepairSelectCondition equipRepairSelectCondition) {
        try {
            Page<EquipRepair> pager = new Page<>(equipRepairSelectCondition.getCurrent(), equipRepairSelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (equipRepairSelectCondition.getOrders() != null) {
                for (OrderItem orderItem : equipRepairSelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<EquipRepair> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new EquipRepair(), parameterMap);
            if (HussarUtils.isNotEmpty(equipRepairSelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = equipRepairSelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<EquipRepair> superQueryGenerator = new SuperQueryGenerator(EquipRepair.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<EquipRepair> list = this.page(pager, queryWrapper);
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            equipRepairPageVO.setData(list.getRecords());
            equipRepairPageVO.setCount(list.getTotal());
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<EquipRepairPageVO> hussarQuery() {
        try {
            EquipRepairPageVO equipRepairPageVO = new EquipRepairPageVO();
            List<EquipRepair> list = this.list();
            equipRepairPageVO.setData(list);
            if (HussarUtils.isNotEmpty(list)) {
                equipRepairPageVO.setCount((long) list.size());
            }
            equipRepairPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(equipRepairPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }
}
